/*
* @Author: dengjiayao
* @Date:   2017-06-26 09:17:32
 * @Last Modified by: songwh
 * @Last Modified time: 2018-04-11 14:01:11
*/

import { modal } from 'vue-strap'
import DatePicker from 'vue-datepicker'

import getDatePickerOption from '../../util/date-picker-option'
import { qiniuUpload, createCastTalkOld, showMsg } from '../../vuex/action'

export default {
  name: 'cast-talk-item-config',
  data() {
    return {
      showModal: false,
      actionType: 'ADD', // ADD or UPDATE
      form: {
        pubDate: '',
        title: '',
        body: '',
        tags: '',
        voiceDuration: '',
        voicePicUrl: '',
        voiceUrl: '',
        speaker: '',
        speakerCompany: '',
        speakerJobTitle: '',
        meetingSummary: ''
      },
      datePickerOption: getDatePickerOption()
    }
  },
  components: {
    'vue-modal': modal,
    'vue-date-picker': DatePicker
  },
  vuex: {
    actions: {
      xUpload: qiniuUpload,
      xCreate: createCastTalkOld,
      xShowMsg: showMsg
    }
  },
  methods: {
    picFileChange(e) {
      var tar = e.target
      if (tar.files.length) {
        var formData = new FormData()
        formData.append('picfile', tar.files[0])
        this.xUpload(formData)
          .then((res) => {
            this.form.voicePicUrl = res.data.url
          })
          .catch((res) => {
            this.xShowMsg({
              type: 'danger',
              content: '上传配图失败。' + res.error_msg
            })
          })
      }
    },
    voiceFileChange(e) {
      var tar = e.target
      if (tar.files.length) {
        var formData = new FormData()
        formData.append('voicefile', tar.files[0])
        this.xUpload(formData)
          .then((res) => {
            this.form.voiceUrl = res.data.url
          })
          .catch((res) => {
            this.xShowMsg({
              type: 'danger',
              content: '上传音频失败。' + res.error_msg
            })
          })
      }
    },
    valid() {
      let {pubDate, title, body, meetingSummary, tags, voiceUrl, voicePicUrl, voiceDuration, speaker, speakerCompany, speakerJobTitle} = this.form
      for (let v of [pubDate, title, body, meetingSummary, tags, voiceUrl, voicePicUrl, voiceDuration, speaker, speakerCompany, speakerJobTitle]) {
        if ((v + '').length === 0) {
          return Promise.reject({
            error_msg: '* 标记的均为必填项'
          })
        }
      }
      return Promise.resolve()
    },
    submit() {
      this.valid()
        .then(() => {
          let {id, pubDate, title, body, meetingSummary, tags, voiceUrl, voicePicUrl, voiceDuration, speaker, speakerCompany, speakerJobTitle} = this.form
          let params = {
            pubDate: new Date(pubDate).getTime(),
            title: title,
            body: body,
            meetingSummary: meetingSummary,
            publishType: -1,
            tags: tags.split(/[,， ]+/),
            voiceInfoRequestDtos: [{
              voicePicUrl: voicePicUrl,
              voiceUrl: voiceUrl,
              duration: voiceDuration
            }],
            speakerInfos: [{
              speaker: speaker,
              company: speakerCompany,
              jobTitle: speakerJobTitle
            }],
            userAuthType: 1
          }
          if (this.actionType === 'UPDATE') {
            params.id = id
          }
          return this.xCreate(params)
        })
        .then(() => {
          this.showModal = false
        })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    }
  }
}
